<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" v-show="false">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:resolve:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:resolve:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:resolve:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:resolve:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="resolveList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="规则分类" align="center" prop="ruleCategory" :formatter="ruleCategoryFormat" />
      <el-table-column label="规则类型" align="center" prop="ruleType" :formatter="ruleTypeFormat" />
      <el-table-column label="编码" align="center" prop="code" />
      <el-table-column label="关键词" align="center" prop="keywords" />
      <el-table-column label="语义" align="center" prop="semanteme" />
      <el-table-column label="算法类型" align="center" prop="algorithm" :formatter="algorithmFormat" />
      <el-table-column label="是否取反" align="center" prop="negateFlag" />
      <el-table-column label="字符匹配方式" align="center" prop="matchType" :formatter="matchTypeFormat" />
      <el-table-column label="目标值" align="center" prop="targetValue" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:resolve:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:resolve:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改规则明细表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="规则分类">
          <el-radio-group v-model="form.ruleCategory">
            <el-radio
              v-for="dict in ruleCategoryOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="规则类型">
          <el-select v-model="form.ruleType" placeholder="请选择规则类型">
            <el-option
              v-for="dict in ruleTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="规则编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入编码" />
        </el-form-item>
        <el-form-item label="算法类型">
          <el-select v-model="form.algorithm" placeholder="请选择算法类型">
            <el-option
              v-for="dict in algorithmOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数据范围">
            <el-button
              type="info"
              icon="el-icon-upload"
              size="mini"
              @click="handleTable"
            >选择表</el-button>


            <el-row>
              <el-table ref="table" :data="tableIds" height="160px" >
                <el-table-column prop="tableName" label="表名称"></el-table-column>
                <el-table-column prop="tableComment" label="表描述"></el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                </el-table-column>
              </el-table>
            </el-row>
        </el-form-item>
        <el-form-item label="关键词" prop="keywords">
          <el-input v-model="form.keywords" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="语义" prop="semanteme">
          <el-input v-model="form.semanteme" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否取反" prop="negateFlag">
          <el-input v-model="form.negateFlag" placeholder="请输入是否取反" />
        </el-form-item>
        <el-form-item label="字符匹配方式">
          <el-select v-model="form.matchType" placeholder="请选择字符匹配方式">
            <el-option
              v-for="dict in matchTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目标值" prop="targetValue">
          <el-input v-model="form.targetValue" placeholder="请输入目标值" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="openTable" width="600px" append-to-body>
      <el-row>
        <el-table ref="table" :data="dbTableList" height="360px" @selection-change="handleSelectionTableChange" >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="tableName" label="表名称"></el-table-column>
          <el-table-column prop="tableComment" label="表描述"></el-table-column>
          <el-table-column prop="createTime" label="创建时间"></el-table-column>
          <el-table-column prop="updateTime" label="更新时间"></el-table-column>
        </el-table>
      </el-row>
      
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitTableForm">确 定</el-button>
        <el-button @click="cancelTable">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listResolve, getResolve, delResolve, addResolve, updateResolve ,listDbTable} from "@/api/system/resolve";

export default {
  name: "Resolve",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      tableIds:[],
      // 非单个禁用
      single: true,
      tableSingle:true,
      // 非多个禁用
      multiple: true,
      tableMultiple:true,
      // 总条数
      total: 0,
      // 规则明细表表格数据
      resolveList: [],
      dbTableList: [],
      tableData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openTable:false,
      // 规则分类,0字符,1数字字典
      ruleCategoryOptions: [],
      // 规则类型字典
      ruleTypeOptions: [],
      // 算法类型字典
      algorithmOptions: [],
      // 字符匹配方式字典
      matchTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ruleCategory: undefined,
        ruleType: undefined,
        code: undefined,
        keywords: undefined,
        semanteme: undefined,
        algorithm: undefined,
        negateFlag: undefined,
        matchType: undefined,
        targetValue: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("drug_rule_category").then(response => {
      this.ruleCategoryOptions = response.data;
    });
    this.getDicts("drug_rule_type").then(response => {
      this.ruleTypeOptions = response.data;
    });
    this.getDicts("algorithm_type").then(response => {
      this.algorithmOptions = response.data;
    });
    this.getDicts("drug_match_type").then(response => {
      this.matchTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询规则明细表列表 */
    getList() {
      this.loading = true;
      listResolve(this.queryParams).then(response => {
        this.resolveList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 规则分类,0字符,1数字字典翻译
    ruleCategoryFormat(row, column) {
      return this.selectDictLabel(this.ruleCategoryOptions, row.ruleCategory);
    },
    // 规则类型字典翻译
    ruleTypeFormat(row, column) {
      return this.selectDictLabel(this.ruleTypeOptions, row.ruleType);
    },
    // 算法类型字典翻译
    algorithmFormat(row, column) {
      return this.selectDictLabel(this.algorithmOptions, row.algorithm);
    },
    // 字符匹配方式字典翻译
    matchTypeFormat(row, column) {
      return this.selectDictLabel(this.matchTypeOptions, row.matchType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        drugRuleId: undefined,
        ruleCategory: "0",
        ruleType: undefined,
        code: undefined,
        keywords: undefined,
        semanteme: undefined,
        algorithm: undefined,
        negateFlag: undefined,
        matchType: undefined,
        targetValue: undefined,
        createTime: undefined,
        createBy: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },

    handleSelectionTableChange(selection) {
      this.tableIds = selection.map(item => item)
      this.tableSingle = selection.length!=1
      this.tableMultiple = !selection.length
    },
    /** 打开导入表弹窗 */
    handleTable() {
      this.reset();
      this.openTable = true;
      this.title = "选择表格属性";
      listDbTable(this.queryParams).then(response => {
        this.dbTableList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    
    submitTableForm(row){
        console.log(this.tableIds);
        this.openTable = false;
    },
    cancelTable(){
        this.openTable = false;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.tableIds = [];
      this.open = true;
      this.title = "添加规则明细表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getResolve(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改规则明细表";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateResolve(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addResolve(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除规则明细表编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delResolve(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/resolve/export', {
        ...this.queryParams
      }, `system_resolve.xlsx`)
    }
  }
};
</script>